package ru.papergames.battleserver.network.clientpackets.webserver;

import ru.papergames.battleserver.model.PaperGameNpcUser;
import ru.papergames.battleserver.model.UsersManager;
import ru.papergames.common.form.NpcPlayerForm;
import ru.papergames.common.netutils.ModelReaderUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : atroshanin
 */
public class AddNewNpcPlayers extends WebServerClientPacket {

    private List<NpcPlayerForm> npcPlayers = new ArrayList<NpcPlayerForm>();

    @Override
    protected void readImpl() {
        int size = readD();
        logger.info("AddNewNpcPlayers.readImpl: Got packet for adding " + size + " npcs.");
        for (int i=0; i<size; i++) {
            NpcPlayerForm npcPlayer = (NpcPlayerForm) ModelReaderUtils.readPlayerFromBuffer(_buf);
            npcPlayers.add(npcPlayer);
        }
    }

    @Override
    protected void runImpl() {
        System.out.println("AddNewNpcPlayers.runImpl");
        logger.info("AddNewNpcPlayers.runImpl: adding NPCS...");
        for (NpcPlayerForm npcPlayer : npcPlayers) {
            UsersManager.getInstance().addConnectedUserToRoom(new PaperGameNpcUser(npcPlayer));
        }
    }

    @Override
    public String getType() {
        return "[c] AddNewPcPlayer[" + ADD_NPC_PLAYERS_PACKET_ID + "]";
    }
}
